/**
 * Created by wangxin on 2017-9-26 10:02:01.
 */

'use strict';
const page = {
    url: {
        loadEventRule: contextData.contextPath + '/work/loadEventRule',
        getCoupon: contextData.contextPath + '/work/getCoupon',
    },
    backgroundImg: $('.mainImg'),
};

page.init = function () {
    page.initElement();
    page.initEvent();
};

page.initElement = function () {
    const thisPage = this;
    var pageMinHeight = () => {
        $('div#container').css('min-height', $(window).height());
    };

    pageMinHeight();
    $(window).bind("resize", pageMinHeight);

    Dolphin.ajax({
        url: thisPage.url.loadEventRule,
        type: "get",
        forceUrl: 'true',
        data: {eventId: contextData.data.eventId},
        onSuccess: function (data) {
            document.title = data.event.eventName;
            var createOrgCode = contextData.userData && contextData.userData.createOrgCode || null;
            var store = data && data.store || null;
            if(!(!createOrgCode || !store || (store && !store.length) || store.find(o=>o.code === createOrgCode))){
                var bg = $("<div style='position:fixed;top: 0;bottom:0;left:0;right:0;text-align: center;padding-top:90%;font-weight:700;color: white;font-size:24px;background: rgba(55,55,55,0.75)'>您不能参加本次活动" +
                    "<br />请咨询相关门店人员</div>");
                $("body").append(bg);
            }
            thisPage.renderCouponList(data.rule);
            thisPage.renderConfig(data.event);
        },
        onError: function (e) {
            alert('网络出错啦，请您重新加载');
        }
    });
};

page.initEvent = function () {
};

page.formatterDate = function (val, formatter) {
    if(val){
        var date = Dolphin.string2date(val, "yyyy-MM-ddThh:mm:ss.");
        date.setHours(date.getHours() + 8);
        return Dolphin.date2string(date, formatter || "yyyy-MM-dd");
    }else{
        return '';
    }
};

page.renderConfig = function (data) {
    if(data.backgroundImg){
        this.backgroundImg.attr('src',Dolphin.path.mengkeUploadPath + data.backgroundImg);
    }else{
        this.backgroundImg.attr('src',Dolphin.path.mengkePublicPath + '/custom/images/prize/main.gif');
    }
    $('#container').css('background', data.backgroundColor || '#ffd4b1');
    $('.couponPanel').css('background', data.panelColor || '#fef0dd');
    var color1 = data.couponColor1 || data.couponColor2 || '#ed5b20';
    var color2 = data.couponColor2 || data.couponColor1 || '#e8370f';
    $('.couponPanel .couponBlock .coupon').css('background', 'linear-gradient(60deg, '+color1+' 10%, '+color2+' 70%)');
    $('.couponPanel .couponBlock .coupon .couponBorder .info > div.getButton').css('color', color2);
};

page.renderCouponList = function(data){
    var thisPage = this;
    var panel = $('#couponPanel');
    data.forEach(function(d){
        panel.append(thisPage.renderCoupon(d));
    });
};
page.renderCoupon = function (data) {
    var thisPage = this;
    var couponBlock = $('<div class="couponBlock">');
    var coupon = $('<div class="coupon">').appendTo(couponBlock);
    var couponBorder = $('<div class="couponBorder">').appendTo(coupon);

    var couponPrice = $('<div class="price">').appendTo(couponBorder);
    var couponPriceUnit = $('<span class="unit">￥</span>').appendTo(couponPrice);
    if(data.giftPrice < 1){
        couponPriceUnit.addClass('percent').html('折');
        couponPrice.append(data.giftPrice * 10);
    }else{
        couponPrice.append(data.giftPrice);
    }

    var couponInfo = $('<div class="info">').appendTo(couponBorder);
    $('<div>').html(data.giftRemark).appendTo(couponInfo);
    $('<div>').html(data.rule).appendTo(couponInfo);
    $('<div class="getButton">').html('立即领取').appendTo(couponInfo).click(function(){
        Dolphin.ajax({
            url:thisPage.url.getCoupon,
            async: false,
            data:{
                prizeId:data._id,
                gameType:'getCouponMultiple',
            },
            success:function(reData){
                $.alert(reData.message);
            }
        });
    });

    return couponBlock;
};

$(function () {
    page.init();
});




